
layui.config({
    base: '/static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index','set','carousel'], function() {
    $ = layui.jquery;
    var table = layui.table
        , admin = layui.admin
        , element = layui.element
        , carousel = layui.carousel
        , layer = layui.layer;


    var articlePage=1;
    var activePage=1;
    var videoPage=1;


    element.on('tab(theTab)', function(data){


        if (data.index == 0){

            $('.tab-icon-0').css('background-image',"url('/static/mobile/img/icon-article-a.png')");
            $('.tab-icon-1').css('background-image',"url('/static/mobile/img/icon-video.png')");
            $('.tab-icon-2').css('background-image',"url('/static/mobile/img/icon-active.png')");
            $('.tab-icon-3').css('background-image',"url('/static/mobile/img/icon-my.png')");

            $.ajax({
                url: "/mobile/article/articleList",
                type: 'post',
                dataType: 'json',
                data: {page:1},
                success: function(data){

                    $('#article-ad-list').html('');
                    $('#article-list-li').html('');

                    var banner_str = '';
                    $.each(data.banner,function(k,v){
                        banner_str += '<div>';
                        banner_str += '<a href="'+ v.banner_link +'">';
                        banner_str += '<img src="'+ v.banner_image +'" alt="">';
                        banner_str += '</a>';
                        banner_str += '</div>';
                    });

                    var article_str = '';
                    $.each(data.article,function(k1,v1){
                        article_str += '<li>';
                        article_str += '<a href="/api/details/details?tid='+v1.id+'">';
                        article_str += '<div class="the-article">';
                        article_str += '<div class="article-l">';
                        article_str += '<h1>'+ v1.article_title +'</h1>';
                        article_str += '<p>' + v1.nickname + '·'+ v1.create_time +'</p>';
                        article_str += '</div>';
                        article_str += '<div class="article-r">';
                        article_str += '<img src="'+ v1.article_thumb +'" alt="">';
                        article_str += '</div>';
                        article_str += '<div class="clear"></div>';
                        article_str += '</div>';
                        article_str += '</a>';
                        article_str += '</li>';
                    });

                    $('#article-ad-list').html(banner_str);
                    $('#article-list-li').html(article_str);

                    var ad_list = carousel.render({
                        elem: '#article-ad'
                        ,height:'100%'
                        ,width: '100%' //设置容器宽度
                        ,arrow: 'always' //始终显示箭头
                        ,arrow: 'none' //切换动画方式
                    });
                }
            });

        }else if(data.index == 1){

            $('.tab-icon-0').css('background-image',"url('/static/mobile/img/icon-article.png')");
            $('.tab-icon-1').css('background-image',"url('/static/mobile/img/icon-video-a.png')");
            $('.tab-icon-2').css('background-image',"url('/static/mobile/img/icon-active.png')");
            $('.tab-icon-3').css('background-image',"url('/static/mobile/img/icon-my.png')");

            $.ajax({
                url: "/mobile/video/videoList",
                type: 'post',
                dataType: 'json',
                data: {page:1},
                success: function(data){

                    if (data.video.length>0){
                        var video_str = '';
                        $.each(data.video,function(k1,v1){
                            var r_num = v1.read_num*1+v1.set_read_num*1;
                            var send_num = v1.send_num*1+v1.set_send_num*1;
                            var collect_num = v1.collect_num*1+v1.set_collect_num*1;

                            video_str += '<li>';
                            video_str += '<a href="http://www.mygalawang.com/mobile/video/video?id='+ v1.id +'">';
                            video_str += '<div class="video-list-main">';
                            video_str += '<div class="video-img">';
                            video_str += '<img src="'+ v1.image +'" alt="">';
                            video_str += '</div>';
                            video_str += '<div class="video-title">';
                            video_str += '<h1>'+ v1.title +'</h1>';
                            video_str += '</div>';
                            video_str += '<div class="video-play">';
                            video_str += '<i></i>';
                            video_str += '</div>';
                            video_str += '<div class="video-info">';
                            video_str += '<div class="video-info-cen">';
                            video_str += '<div class="video-info-l">';
                            video_str += '<i></i>';
                            video_str += '<span id="look_num">'+ r_num +'</span>次观看';
                            video_str += '</div>';
                            video_str += '<div class="video-info-r">';
                            video_str += formatSeconds(v1.v_time);
                            video_str += '</div>';
                            video_str += '</div>';
                            video_str += '</div>';
                            video_str += '</div>';
                            video_str += '</a>';
                            video_str += '<div class="video-user">';
                            video_str += '<div class="video-user-name">'+ v1.nickname +'·'+ v1.create_time +'</div>';
                            video_str += '<div class="video-send-num"><i></i>'+send_num+'</div>';
                            video_str += '<div class="video-collect-num"><i></i>'+ collect_num +'</div>';
                            video_str += '<div class="clear"></div>';
                            video_str += '</div>';
                            video_str += '<div class="need-height-4"></div>';
                            video_str += '</li>';
                        });

                        $('#video-list-li').html(video_str);

                    }else{

                        $('#video-next-page').html('没有更多内容了！');

                    }

                }
            });

        }else if(data.index == 3){

            $('.tab-icon-0').css('background-image',"url('/static/mobile/img/icon-article.png')");
            $('.tab-icon-1').css('background-image',"url('/static/mobile/img/icon-video.png')");
            $('.tab-icon-2').css('background-image',"url('/static/mobile/img/icon-active-a.png')");
            $('.tab-icon-3').css('background-image',"url('/static/mobile/img/icon-my.png')");

            $.ajax({
                url: "/mobile/activity/activityList",
                type: 'post',
                dataType: 'json',
                data: {page:1},
                success: function(data){


                    $('#activity-list-li').html('');


                    var activity_str = '';
                    $.each(data.activity,function(k1,v1){
                        activity_str += '<li>';
                        activity_str += '<div class="active-img">';
                        activity_str += '<a href="/api/details/activity_details?tid='+v1.id+'">';
                        activity_str += '<img src="'+v1.activity_cover+'" alt="">';
                        activity_str += '</a>';
                        activity_str += '</div>';
                        activity_str += '<div class="active-info">';
                        activity_str += ' <h1>'+v1.activity_title+'</h1>';
                        activity_str += ' <p class="active-time"><i></i>'+v1.activity_start_time+'<span></span>'+v1.week+'</p>';
                        activity_str += ' <p class="active-place"><i></i>'+v1.activity_address+'</p>';
                        if(v1.is_pass==1){
                            activity_str += '<a href="javascript:;" class="active_go_detial_disable">已结束</a>';

                        }else{
                            activity_str += ' <a href="javascript:;" class="active_go_detial" onclick="sign_up()">报名参加</a>';
                        }
                        activity_str += '</div>';
                        activity_str += '<div class="need-height-4"></div>';
                        activity_str += '</li>';
                    });


                    $('#activity-list-li').html(activity_str);

                }
            });
        }else if(data.index == 2){

            $('.tab-icon-0').css('background-image',"url('/static/mobile/img/icon-article.png')");
            $('.tab-icon-1').css('background-image',"url('/static/mobile/img/icon-video.png')");
            $('.tab-icon-2').css('background-image',"url('/static/mobile/img/icon-active.png')");
            $('.tab-icon-3').css('background-image',"url('/static/mobile/img/icon-my-a.png')");

            if($('#session').val()==""){
                window.location.href="/mobile/login/login";
            }
        }
        
    });

    $.ajax({
        url: "/mobile/article/articleList",
        type: 'post',
        dataType: 'json',
        data: {page:1},
        success: function(data){
            var banner_str = '';
            $.each(data.banner,function(k,v){
                banner_str += '<div>';
                banner_str += '<a href="'+ v.banner_link +'">';
                banner_str += '<img src="'+ v.banner_image +'" alt="">';
                banner_str += '</a>';
                banner_str += '</div>';
            });

            var article_str = '';
            $.each(data.article,function(k1,v1){
                article_str += '<li>';
                article_str += '<a href="/api/details/details?tid='+v1.id+'">';
                article_str += '<div class="the-article">';
                article_str += '<div class="article-l">';
                article_str += '<h1>'+ v1.article_title +'</h1>';
                article_str += '<p>' + v1.nickname + '·'+ v1.create_time +'</p>';
                article_str += '</div>';
                article_str += '<div class="article-r">';
                article_str += '<img src="'+ v1.article_thumb +'" alt="">';
                article_str += '</div>';
                article_str += '<div class="clear"></div>';
                article_str += '</div>';
                article_str += '</a>';
                article_str += '</li>';
            });

            $('#article-ad-list').html(banner_str);
            $('#article-list-li').html(article_str);

            var ad_list = carousel.render({
                elem: '#article-ad'
                ,height:'100%'
                ,width: '100%' //设置容器宽度
                ,arrow: 'none' //切换动画方式
            });
        }
    });

    $('#article-next-page').bind('click',function(){

        articlePage++;
        $.ajax({
            url: "/mobile/article/articleListPage",
            type: 'post',
            dataType: 'json',
            data: {page:articlePage},
            success: function(data){
                
                if (data.article.length>0){

                    var article_str = '';
                    $.each(data.article,function(k1,v1){
                        article_str += '<li>';
                        article_str += '<a href="/api/details/details?tid='+v1.id+'">';
                        article_str += '<div class="the-article">';
                        article_str += '<div class="article-l">';
                        article_str += '<h1>'+ v1.article_title +'</h1>';
                        article_str += '<p>' + v1.nickname + '·'+ v1.create_time +'</p>';
                        article_str += '</div>';
                        article_str += '<div class="article-r">';
                        article_str += '<img src="'+ v1.article_thumb +'" alt="">';
                        article_str += '</div>';
                        article_str += '<div class="clear"></div>';
                        article_str += '</div>';
                        article_str += '</a>';
                        article_str += '</li>';
                    });

                    $('#article-list-li').append(article_str);

                }else{

                    $('#article-next-page').html('没有更多内容了！');

                }

            }
        });

    });

    $('#activity-next-page').bind('click',function(){

        activePage++;
        $.ajax({
            url: "/mobile/activity/activityListPage",
            type: 'post',
            dataType: 'json',
            data: {page:activePage},
            success: function(data){

                if (data.activity.length>0){

                    var activity_str = '';
                    $.each(data.activity,function(k1,v1){
                        activity_str += '<li>';
                        activity_str += '<div class="active-img">';
                        activity_str += '<a href="/api/details/activity_details?tid='+v1.id+'">';
                        activity_str += '<img src="'+v1.activity_cover+'" alt="">';
                        activity_str += '</a>';
                        activity_str += '</div>';
                        activity_str += '<div class="active-info">';
                        activity_str += ' <h1>'+v1.activity_title+'</h1>';
                        activity_str += ' <p class="active-time"><i></i>'+v1.activity_start_time+'<span></span>'+v1.week+'</p>';
                        activity_str += ' <p class="active-place"><i></i>'+v1.activity_address+'</p>';
                        if(v1.is_pass==1){
                            activity_str += '<a href="javascript:;" class="active_go_detial_disable">已结束</a>';
                        }else{
                            activity_str += ' <a href="" class="active_go_detial" onclick="sign_up()">报名参加</a>';
                        }
                        activity_str += '</div>';
                        activity_str += '<div class="need-height-4"></div>';
                        activity_str += '</li>';
                    });

                    $('#activity-list-li').html(activity_str);

                }else{

                    $('#activity-next-page').html('没有更多内容了！');

                }

            }
        });

    });

    $('#video-next-page').bind('click',function(){

        videoPage++;
        $.ajax({
            url: "/mobile/video/videoList",
            type: 'post',
            dataType: 'json',
            data: {page:videoPage},
            success: function(data){

                if (data.video.length>0){

                    var video_str = '';
                    $.each(data.video,function(k1,v1){
                        var r_num = v1.read_num*1+v1.set_read_num*1;
                        var send_num = v1.send_num*1+v1.set_send_num*1;
                        var collect_num = v1.collect_num*1+v1.set_collect_num*1;

                        video_str += '<li>';
                        video_str += '<a href="http://www.mygalawang.com/mobile/video/video?id='+ v1.id +'">';
                        video_str += '<div class="video-list-main">';
                        video_str += '<div class="video-img">';
                        video_str += '<img src="'+ v1.image +'" alt="">';
                        video_str += '</div>';
                        video_str += '<div class="video-title">';
                        video_str += '<h1>'+ v1.title +'</h1>';
                        video_str += '</div>';
                        video_str += '<div class="video-play">';
                        video_str += '<i></i>';
                        video_str += '</div>';
                        video_str += '<div class="video-info">';
                        video_str += '<div class="video-info-cen">';
                        video_str += '<div class="video-info-l">';
                        video_str += '<i></i>';
                        video_str += '<span id="look_num">'+ r_num +'</span>次观看';
                        video_str += '</div>';
                        video_str += '<div class="video-info-r">';
                        video_str += formatSeconds(v1.v_time);
                        video_str += '</div>';
                        video_str += '</div>';
                        video_str += '</div>';
                        video_str += '</div>';
                        video_str += '</a>';
                        video_str += '<div class="video-user">';
                        video_str += '<div class="video-user-name">'+ v1.nickname +'·'+ v1.create_time +'</div>';
                        video_str += '<div class="video-send-num"><i></i>'+send_num+'</div>';
                        video_str += '<div class="video-collect-num"><i></i>'+ collect_num +'</div>';
                        video_str += '<div class="clear"></div>';
                        video_str += '</div>';
                        video_str += '<div class="need-height-4"></div>';
                        video_str += '</li>';
                    });

                    $('#video-list-li').append(video_str);
                }else{
                    $('#video-next-page').html('没有更多内容了！');
                }
            }
        });

    });

    function formatSeconds(value) {
        var secondTime = parseInt(value);// 秒
        var minuteTime = 0;// 分
        var hourTime = 0;// 小时
        if(secondTime > 60) {//如果秒数大于60，将秒数转换成整数
            //获取分钟，除以60取整数，得到整数分钟
            minuteTime = parseInt(secondTime / 60);
            //获取秒数，秒数取佘，得到整数秒数
            secondTime = parseInt(secondTime % 60);
            //如果分钟大于60，将分钟转换成小时
            if(minuteTime > 60) {
                //获取小时，获取分钟除以60，得到整数小时
                hourTime = parseInt(minuteTime / 60);
                //获取小时后取佘的分，获取分钟除以60取佘的分
                minuteTime = parseInt(minuteTime % 60);
            }
        }
        var result = "" + parseInt(secondTime);

        if(minuteTime > 0) {
            result = "" + parseInt(minuteTime) + ":" + result;
        }
        if(hourTime > 0) {
            result = "" + parseInt(hourTime) + ":" + result;
        }
        return result;
    }

});

//点击报名
function sign_up() {
   layer.msg('报名请下载app');
}
